import General from '@/api/general'
import Login from '@/api/login'
import Public from '@/api/public'
import mapPlugin from '@/libs/mapPlugin'
import { mapState } from 'vuex'
export default {
  data() {
    return {}
  },
  computed: {
    ...mapState({
      user: state => state.userInfo
    })
  },
  created() {},
  methods: {
    // 存储数据并获取menu
    setData(data) {
      // 重置超时状态
      this.$store.commit('setTimeOut', false)
      // vuex统一存储登录返回的用户信息
      this.$store.commit('setUserInfo', data)
      localStorage.setItem('userInfo', JSON.stringify(data))
      localStorage.setItem('token', data.token)
      if (data.gridPersonType === '5') {
        this.$router.push('/cockpit/home')
      } else {
        this.$router.push('/home')
      }
      this.sendLocation()
      setTimeout(() => {
        this.getMenu()
      }, 10)
    },
    // 根据域名获取行政区划编码
    async getRegionCode() {
      await General.Region.getRegionCode({
        ip: document.location.host.indexOf('localhost') > -1 ? '188.188.33.24:86' : document.location.host
      }).then(res => {
        this.$store.commit('setRegionCode', res.data.data || '360100')
      })
    },
    getMenu() {
      Login.Login.getMenuTree()
        .then(res => {
          const resData = res.data.data
          this.$store.commit('setUserMenus', resData)
          localStorage.setItem('userMenus', JSON.stringify(resData))
          const menuPath = []
          for (let i = 0; i < resData.length; i++) {
            menuPath.push(resData[i].url)
            for (let j = 0; j < resData[i].child.length; j++) {
              menuPath.push(resData[i].child[j].url)
            }
          }
          localStorage.setItem('menus', menuPath)
        })
        .catch(() => {})
    },
    // 获取用户位置并发送
    sendLocation() {
      if (this.user && this.user.appCode) {
        mapPlugin.geolocation(result => {
          const lngLat = result.position
          Public.User.positioningLocation({
            longitude: lngLat.lng,
            latitude: lngLat.lat,
            appCode: this.user.appCode,
            gridCode: this.user.gridCode,
            regionCode: this.user.regionCode,
            phone: this.user.phone,
            gridPersonType: this.user.gridPersonType,
            gridName: this.user.gridName,
            gridPersonCode: this.user.gridPersonCode,
            gridPersonName: this.user.gridPersonName
          }).then(res => {
            console.log(res)
          })
        }, true)
      }
    }
  }
}
